package com.sinaapp.gavinzhang.jianzhioffer;

import org.junit.Test;

import java.util.Stack;

/**
 * 判断一个数组的出栈顺序是否正确
 * Created by gavin on 15-9-16.
 */
public class StackJudge {

    @Test
    public void StackJudge()
    {
        char s[] = {'a','b','c','d','e'};
        //String tmp1 = "abcde";
        String tmp1 = "dacbe";

        Stack<Character> stack = new Stack<Character>();
        int j = 0;
        int i = 0;
        stack.push(s[i]);
        while (stack.size() != 0)
        {
            char tt = stack.pop();
            if (tt == tmp1.toCharArray()[j])
            {
                j++;
            }else {
                if (i == s.length-1)
                {
                    System.out.println("error");
                    return;
                }
                stack.push(tt);
                stack.push(s[++i]);
            }
        }
        System.out.println("ok");


    }

}
